home *** CD-ROM | disk | FTP | other *** search
/ C & C++ Multimedia Cyber Classroom / C and C++ Multimedia Cyber Classroom (Prentice Hall) (1998).iso / cpphtp2 / code.jar / code / ch09 / fig09_07.txt < prev    next >
Text File  |  1998-02-27  |  2KB  |  102 lines

  1. 1   // Fig. 9.7: point2.h
  2. 2   // Definition of class Point
  3. 3   #ifndef POINT2_H
  4. 4   #define POINT2_H
  5. 5   
  6. 6   class Point {
  7. 7   public:
  8. 8      Point( int = 0, int = 0 );  // default constructor
  9. 9      ~Point();    // destructor
  10. 10  protected:      // accessible by derived classes
  11. 11     int x, y;    // x and y coordinates of Point
  12. 12  };
  13. 13  
  14. 14  #endif
  15. 15  
  16. 16  
  17. 17  // Fig. 9.7: point2.cpp
  18. 18  // Member function definitions for class Point
  19. 19  #include <iostream.h>
  20. 20  #include "point2.h"
  21. 21  
  22. 22  // Constructor for class Point
  23. 23  Point::Point( int a, int b )
  24. 24  {
  25. 25     x = a;
  26. 26     y = b;
  27. 27  
  28. 28     cout << "Point  constructor: "
  29. 29          << '[' << x << ", " << y << ']' << endl;
  30. 30  }
  31. 31  
  32. 32  // Destructor for class Point
  33. 33  Point::~Point()
  34. 34  {
  35. 35     cout << "Point  destructor:  "
  36. 36          << '[' << x << ", " << y << ']' << endl;
  37. 37  }
  38. 38  
  39. 39  
  40. 40  // Fig. 9.7: circle2.h
  41. 41  // Definition of class Circle
  42. 42  #ifndef CIRCLE2_H
  43. 43  #define CIRCLE2_H
  44. 44  
  45. 45  #include "point2.h"
  46. 46  
  47. 47  class Circle : public Point {
  48. 48  public:
  49. 49     // default constructor
  50. 50     Circle( double r = 0.0, int x = 0, int y = 0 );
  51. 51  
  52. 52     ~Circle();     
  53. 53  private:
  54. 54     double radius; 
  55. 55  };
  56. 56  
  57. 57  #endif
  58. 58  
  59. 59  
  60. 60  // Fig. 9.7: circle2.cpp 
  61. 61  // Member function definitions for class Circle
  62. 62  #include "circle2.h"
  63. 63  
  64. 64  // Constructor for Circle calls constructor for Point
  65. 65  Circle::Circle( double r, int a, int b )
  66. 66     : Point( a, b )   // call base-class constructor
  67. 67  {
  68. 68     radius = r;  // should validate
  69. 69     cout << "Circle constructor: radius is "
  70. 70          << radius << " [" << x << ", " << y << ']' << endl;
  71. 71  }
  72. 72  
  73. 73  // Destructor for class Circle
  74. 74  Circle::~Circle()
  75. 75  {
  76. 76     cout << "Circle destructor:  radius is "
  77. 77          << radius << " [" << x << ", " << y << ']' << endl;
  78. 78  }
  79. 79  
  80. 80  
  81. 81  // Fig. 9.7: fig09_07.cpp
  82. 82  // Demonstrate when base-class and derived-class
  83. 83  // constructors and destructors are called.
  84. 84  #include <iostream.h>
  85. 85  #include "point2.h"
  86. 86  #include "circle2.h"
  87. 87  
  88. 88  int main()
  89. 89  {
  90. 90     // Show constructor and destructor calls for Point
  91. 91     {
  92. 92        Point p( 11, 22 );
  93. 93     }
  94. 94  
  95. 95     cout << endl;
  96. 96     Circle circle1( 4.5, 72, 29 );
  97. 97     cout << endl;
  98. 98     Circle circle2( 10, 5, 5 );
  99. 99     cout << endl;
  100. 100    return 0;
  101. 101 }
  102.